﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;

namespace YJKRevitKernel.FaceCalculateUtility
{
	public class ElementPacker : BasePacker
	{
		public Element ThisElement
		{
			get
			{
				return this.m_thisElement;
			}
			private set
			{
				this.m_thisElement = value;
			}
		}

		internal ElementPacker(Element input)
		{
			this.ThisElement = input;
			foreach (PlanarFace inputFace in this.GetAllPlanarFace())
			{
				base.ThisLstFace.Add(new PlanarFacePacker(inputFace, this));
			}
		}

		private List<PlanarFace> GetAllPlanarFace()
		{
			List<PlanarFace> list = new List<PlanarFace>();
			List<Solid> list2 = new List<Solid>();
			Options options = new Options();
			GeometryElement useGermetry = this.ThisElement.get_Geometry(options);
			this.GetLstSolid(useGermetry, ref list2, null);
			foreach (Solid solid in list2)
			{
				list.AddRange(BasePacker.GetSolidPlanarFace(solid));
			}
			return list;
		}

		private void GetLstSolid(GeometryElement useGermetry, ref List<Solid> lstSolid, Transform useTransform = null)
		{
			foreach (GeometryObject geometryObject in useGermetry)
			{
				if (null != geometryObject as Solid)
				{
					Solid solid = geometryObject as Solid;
					if (useTransform != null)
					{
						solid = SolidUtils.CreateTransformed(solid, useTransform);
					}
					lstSolid.Add(solid);
				}
				else if (null != geometryObject as GeometryInstance)
				{
					GeometryInstance geometryInstance = geometryObject as GeometryInstance;
					Transform transform = geometryInstance.Transform;
					if (useTransform != null)
					{
						transform = useTransform.Multiply(transform);
					}
					GeometryElement symbolGeometry = geometryInstance.SymbolGeometry;
					this.GetLstSolid(symbolGeometry, ref lstSolid, transform);
				}
			}
		}

		private Element m_thisElement;
	}
}
